#include "singlelooplist.h"
#include <iostream>

using namespace singlelooplist;

ListElmt::ListElmt(int data)
{
    this->m_data = data;
    this->m_next = NULL;
}

void ListElmt::SetData(int d)
{
    this->m_data = d;
}

void ListElmt::SetNext(ListElmt* e)
{
    this->m_next = e;
}

int ListElmt::GetData()
{
    return this->m_data;
}

ListElmt* ListElmt::GetNext()
{
    return this->m_next;
}

SingleLoopList::SingleLoopList()
{
    this->m_head = NULL;
    this->m_size = 0;
}

SingleLoopList::~SingleLoopList()
{
    if (this->m_head != NULL)
    {
        ListElmt* del = NULL;
        for(int i = 0; i < this->m_size; ++i)
        {
             del = this->m_head;
             this->m_head = this->m_head->GetNext();
             delete del;
        }
    }
}

int SingleLoopList::GetSize()
{
    return this->m_size;
}

ListElmt* SingleLoopList::ListHead()
{
    return this->m_head;
}

ListElmt* SingleLoopList::ListDelNext(ListElmt* e)
{
    if (this->m_size == 0)
    {
        return NULL;
    }

    ListElmt* del = NULL;
    if (e->GetNext() == e)
    {
        del = e;
        this->m_head = NULL;
    }
    else
    {
        del = e->GetNext();
        e->SetNext(del->GetNext());
    }

    --this->m_size;
    return del;
}

void SingleLoopList::ListInsert(ListElmt* e, ListElmt* i)
{
    if (i == NULL)
    {
        return;
    }

    if (this->m_size == 0)
    {
        this->m_head = i;
        i->SetNext(i);
    }
    else
    {
        i->SetNext(e->GetNext());
        e->SetNext(i);
    }
    ++this->m_size;
}

void singlelooplist::PrintSingleLoopList(SingleLoopList* l)
{
     std::cout << "cur data: ";
     int s = l->GetSize();
     ListElmt* head = l->ListHead();
     for(int i = 0; i < s; ++i)
     {
         std::cout << head->GetData() << " ";
         head = head->GetNext();
     }
     std::cout << std::endl;
     std::cout << "singlelooplist size: " << l->GetSize() << std::endl;
}


